﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TireService.Data;
using System.Web.Security;

namespace TireService.Web.View
{
    public partial class Hjulskift : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {

            try
            {
                // Instantiate database connection
                TireServiceDB db = new TireServiceDB();

                List<ServiceCarScheduleDetail> details = db.ServiceCarScheduleDetails.Where(p => p.CarId != null & p.WorkCompleted == false & p.ScheduleDate < Convert.ToDateTime("01-02-2012")).ToList();

                //ServiceCarScheduleDetail detail = db.ServiceCarScheduleDetails.SingleOrDefault(p => p.CarId == Convert.ToInt32(txtCarId.Text) & p.WorkCompleted == false);

                foreach (ServiceCarScheduleDetail detail in details)
                {
                    try
                    {
                        // Mark detail as completed
                        detail.WorkCompleted = true;

                        // Update car
                        Car car = db.GetCar(Convert.ToInt32(detail.CarId));

                        car.LastTireSetChangeDate = detail.ScheduleDate;
                        car.NextTireSetChangePlanningDate = detail.ScheduleDate.AddMonths(5);
                        car.CarStatusId = (int)Shared.CarStatus.TireChanged;

                        // Fetch subscription (to see if balancing is included in customer subscription)
                        CustomerSubscription subscription = db.GetCustomerSubscription(Convert.ToInt32(car.CustomerSubscriptionId));

                        string subscriptionnumber = car.CustomerId.ToString() + "-" + subscription.SubscriptionId.ToString() + "-" + car.CustomerSubscriptionId.ToString();

                        // Update tiresets 
                        // Update from tireset
                        TireSet FromTireset = db.GetTireSet(Convert.ToInt32(detail.FromTireSetId));
                        TireSet ToTireset = db.GetTireSet(Convert.ToInt32(detail.ToTireSetId));

                        // Create tiresetlog for fromtireset
                        TireSetLogBLL bll = new TireSetLogBLL();
                        bll.CreateTireSetLog(db, FromTireset, DateTime.Now, FromTireset.TireSetStatusId, (int)Shared.TireSetStatus.OnStock, new Guid(Membership.GetUser().ProviderUserKey.ToString()), "Hjul afmonteret og lagt på lager", null, null, null, null, ToTireset.ContainerCargoRoomId);

                        // Create tiresetlog for totireset
                        bll.CreateTireSetLog(db, ToTireset, DateTime.Now, ToTireset.TireSetStatusId, (int)Shared.TireSetStatus.MountedOnCar, new Guid(Membership.GetUser().ProviderUserKey.ToString()), "Hjul påmonteret kundens bil", null, null, null, null, null);

                        // Placeholder properties for tiresetstatus and containercargoroomid
                        int containercargoroomid = Convert.ToInt32(ToTireset.ContainerCargoRoomId);

                        // Update totireset according to log
                        ToTireset.TireSetStatusId = (int)Shared.TireSetStatus.MountedOnCar;
                        ToTireset.ContainerCargoRoomId = null;

                        // Update fromtireset according to log
                        FromTireset.ContainerCargoRoomId = containercargoroomid;
                        FromTireset.TireSetStatusId = (int)Shared.TireSetStatus.OnStock;
                        if (subscription.BalancingIncluded) FromTireset.NeedBalancing = true;

                        // Commit changes to database
                        db.SubmitChanges();
                    }
                    catch (Exception)
                    {
                        
                    }
                    
                }

                // Dispose database connection
                db.Dispose();

                Response.Write("Det lykkedes :-) ");
            }
            catch (Exception ex)
            {
                Response.Write("Fejl opstod: " + ex.Message);
            }
            
        }
    }
}